<?php
/******************************************************************************
*                                                                             *
* Steak Desk v0.2                                                             *
*                                                                             *
* Copyright (c) 2011 Andrew D. Zonenberg.                                     *
* All rights reserved.                                                        *
*                                                                             *
* Redistribution and use in source and binary forms, with or without modifi-  *
* cation, are permitted provided that the following conditions are met:       *
*                                                                             *
*    * Redistributions of source code must retain the above copyright notice  *
*      this list of conditions and the following disclaimer.                  *
*                                                                             *
*    * Redistributions in binary form must reproduce the above copyright      *
*      notice, this list of conditions and the following disclaimer in the    *
*      documentation and/or other materials provided with the distribution.   *
*                                                                             *
*    * Neither the name of the author nor the names of its contributors may be*
*      used to endorse or promote products derived from this software without *
*      specific prior written permission.                                     *
*                                                                             *
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS OR IMPLIED *
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF        *
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN     *
* NO EVENT SHALL THE AUTHOR BE HELD LIABLE FOR ANY DIRECT, INDIRECT,          *
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT*
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, *
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF   *
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING        *
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS          *
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                *
*                                                                             *
*******************************************************************************/

/**
	@brief Returns a bunch of <option> tags corresponding to an enumeration
 */
function enum_list($ename, $selection = 0)
{
	global $g_tplvars;
	
	//First, find the enum
	$r = dbquery('select * from enums where `name` = \'' . $ename . '\'');
	if(mysql_num_rows($r) != 1)
		return '';
	$e = mysql_fetch_object($r);
	
	//and then dump all of its data
	$r = dbquery('select * from enumvals where `eid` = \'' . $e->id . '\' order by name asc');
	$ret = '';
	while($line = mysql_fetch_object($r))
	{
		if($line->val == $selection)
			$g_tplvars['selected'] = 'selected=\'1\'';
		else
			$g_tplvars['selected'] = '';
			
		$g_tplvars['value'] = $line->val;
		$g_tplvars['name'] = $line->name;
		$ret .= templatize('../templates/enum-selector-line.html');
	}
	return $ret;
}

/**
	@brief Converts an int to an enum name
	
	@param ename Enum name
	@param value Value
 */
function enum_value_to_name($ename, $value)
{
	//First, find the enum
	$r = dbquery('select * from enums where `name` = \'' . $ename . '\'');
	if(mysql_num_rows($r) != 1)
		return '';
	$e = mysql_fetch_object($r);
	
	//and then dump the fun stuff
	$r = dbquery('select * from enumvals where `eid` = \'' . $e->id . '\' and `val` = \'' . $value . '\'');
	if(mysql_num_rows($r) != 1)
		return '';
	$v = mysql_fetch_object($r);
	return $v->name;
}

/**
	@brief Converts an enum name to an int
	
	@param ename Enum name
	@param name Name of the value to conver
 */
function enum_name_to_value($ename, $name)
{
	//First, find the enum
	$r = dbquery('select * from enums where `name` = \'' . $ename . '\'');
	if(mysql_num_rows($r) != 1)
		return '';
	$e = mysql_fetch_object($r);
	
	//and then dump the fun stuff
	$r = dbquery('select * from enumvals where `eid` = \'' . $e->id . '\' and `name` = \'' . $name . '\'');
	if(mysql_num_rows($r) != 1)
		return '';
	$v = mysql_fetch_object($r);
	return $v->val;
}

?>
